package com.dianrui.agent.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidkun.xtablayout.XTabLayout;
import com.dianrui.agent.R;
import com.dianrui.agent.adapter.DeviceListPageAdapter;
import com.dianrui.agent.event.DeviceLockRfresh;
import com.dianrui.agent.event.DeviceLowVolRfresh;
import com.dianrui.agent.event.DeviceNormalRfresh;
import com.dianrui.agent.event.DeviceToubleRfresh;
import com.dianrui.agent.event.DeviceRedDot;
import com.dianrui.agent.util.MyUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class DeviceListActivity extends BaseActivity {

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.xTablayout)
    XTabLayout tablayout;
    @BindView(R.id.pager)
    ViewPager pager;
    @BindView(R.id.top_right_img)
    ImageView topRightImg;



    @Override
    public int getContentViewId() {
        return R.layout.device_list;
    }

    @Override
    public void initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        title.setText("设备列表");
        topRightImg.setImageResource(R.mipmap.logo_search);
        DeviceListPageAdapter adapter = new DeviceListPageAdapter(getSupportFragmentManager(), DeviceListActivity.this);
        pager.setAdapter(adapter);
        tablayout.setupWithViewPager(pager);
        pager.setOffscreenPageLimit(4);
        for (int i = 0; i < tablayout.getTabCount(); i++) {
            XTabLayout.Tab tab = tablayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(adapter.getTabView(i));
            }
        }

    }


    //我的订单上面的红点
    @SuppressLint("SetTextI18n")
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(DeviceRedDot deviceData) {
        List<Integer> numberList = deviceData.numberList;
        for (int i = 0; i < tablayout.getTabCount(); i++) {
            XTabLayout.Tab tab = tablayout.getTabAt(i);
            TextView hasNewMsg1 = tab.getCustomView().findViewById(R.id.tab_msg_num1);
            if (0== i) {
                if (numberList.get(0)>9999) {
                    hasNewMsg1.setVisibility(View.VISIBLE);
                    hasNewMsg1.setText("9999+");
                }else if (numberList.get(0)<=0) {
                    hasNewMsg1.setVisibility(View.GONE);
                }else {
                    hasNewMsg1.setText(deviceData.numberList.get(0)+" ");
                    hasNewMsg1.setVisibility(View.VISIBLE);
                }
            }
            if (1== i) {
                if (numberList.get(1)>9999) {
                    hasNewMsg1.setVisibility(View.VISIBLE);
                    hasNewMsg1.setText("9999+");
                }else if (numberList.get(1)<=0) {
                    hasNewMsg1.setVisibility(View.GONE);
                }else {
                    hasNewMsg1.setText(deviceData.numberList.get(1)+" ");
                    hasNewMsg1.setVisibility(View.VISIBLE);
                }
            }
            if (2== i) {
                if (numberList.get(2)>9999) {
                    hasNewMsg1.setVisibility(View.VISIBLE);
                    hasNewMsg1.setText("9999+");
                }else if (numberList.get(2)<=0) {
                    hasNewMsg1.setVisibility(View.GONE);
                }else {
                    hasNewMsg1.setText(deviceData.numberList.get(2)+" ");
                    hasNewMsg1.setVisibility(View.VISIBLE);
                }
            }
            if (3 == i) {
                if (numberList.get(3)>9999) {
                    hasNewMsg1.setVisibility(View.VISIBLE);
                    hasNewMsg1.setText("9999+");
                }else if (numberList.get(3)<=0) {
                    hasNewMsg1.setVisibility(View.GONE);
                }else {
                    hasNewMsg1.setText(deviceData.numberList.get(3)+" ");
                    hasNewMsg1.setVisibility(View.VISIBLE);
                }
            }

        }
    }


    @OnClick({R.id.back, R.id.top_right_img})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.top_right_img:
                startActivity(new Intent(DeviceListActivity.this,SearchDeviceActivity.class));
                break;
                default:
                    break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }
}
